//
//  T3FAnnotation+Wait.swift
//  Pods
//
//  Created by meiqs1 on 2025/1/26.
//

import UIKit
import SensitiveContentAnalysis
import t3_assets
import Kingfisher

///等待接单标注图标(带气泡)
class T3FWaitAnnotationView: UIView, T3FAnnotationView {
    var annotationDidClicked: (([String : Any]?) -> Void)?
    
    struct Options {
        /// 气泡文案
        public var text: String?
        /// 大头针图片
        public var imageName: String = "location"
        /// 水波纹动画倍数
        public var waterAnimationMultiple: CGFloat = 1.0
        /// 水波纹颜色
        public var pulesColor: UIColor?
        /// 地址信息
        public var address: String?
        /// 限停信息
        public var limitStopTips: String?
        /// 步行导览的点位信息
        public var guideMainUrl: String?
        /// 步行导览跳转链接
        public var guidePageUrl: String?
        
        public var image: UIImage?

        init?(json: [String : Any]) {
            if let text = json["bubbleText"] as? String {
                self.text = text
            } else {
                self.text = nil
            }

            if let image = json["image"] as? String {
                self.imageName = image
            } else {
                self.imageName = "location"
            }
            if let anImage = UIImage(named: self.imageName) {
                self.image = anImage
            }
            
            if let waterMultiple = json["waterAnimationMultiple"] as? Double {
                self.waterAnimationMultiple = CGFloat(waterMultiple)
            } else {
                self.waterAnimationMultiple = 1.0
            }
            if let pulesColor = json["pulesColor"] as? String {
                self.pulesColor = UIColor(inner: pulesColor)
            } else {
                self.pulesColor = nil
            }
            if let address = json["address"] as? String {
                self.address = address
            } else {
                self.address = nil
            }
            if let limitStopTips = json["limitStopTips"] as? String {
                self.limitStopTips = limitStopTips
            } else {
                self.limitStopTips = nil
            }
            if let guideMainUrl = json["guideMainUrl"] as? String {
                self.guideMainUrl = guideMainUrl
            } else {
                self.guideMainUrl = nil
            }
            if let guidePageUrl = json["guidePageUrl"] as? String {
                self.guidePageUrl = guidePageUrl
            } else {
                self.guidePageUrl = nil
            }
            
            
        }
        
        public var realAnimationMultiple: CGFloat {
            get {
                if waterAnimationMultiple > 2 {
                    return 2.25
                } else if waterAnimationMultiple > 1 {
                    return 1.7
                }
                return 1.0
            }
        }
    }

//    private var guideInfo: GuideInfo?
    
    // swiftlint:disable implicitly_unwrapped_optional
//    init() {
//        // swiftlint:enable implicitly_unwrapped_optional
////        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
//        super.init(frame: CGRect.zero)
//        self.setupUI()
//
////        self.setupNetPing()
//
//    }
    
    var options: Options

    init(options: Options) {
        self.options = options
        super.init(frame: .zero)
        self.setupUI()
        self.updateData()
    }
    
    func updateData() {
        let imageHeight: CGFloat = options.image?.size.height ?? 0
        
        guard let originView = superview else { return }
//        let imageHeight = options.imageName?.size.height  ?? 0
//        originView.set(centerOffset: CGPoint(x: 0, y: -imageHeight / 2))
//        self.guideInfo = annotation.guideInfo
        
        imageView.snp.makeConstraints { make in
            make.edges.equalTo(originView)
        }
        imageView.image = options.image
        
        waterView.snp.makeConstraints { make in
            make.centerX.equalTo(originView)
            make.centerY.equalTo(originView).offset(imageHeight / 2)
            make.width.height.equalTo(362)
        }

//        updateAnnotationImage(with: annotation.imageName)
        updateText(with: options.text)
        updateWaterAnimation(with: options.realAnimationMultiple,
                             pulesColor: options.pulesColor)
        updateAddress(with: options.address)
        updateLimitStopTips(tips: options.limitStopTips)
        updateWalkNav()
        
        placeLabel.snp.remakeConstraints {
            $0.top.equalTo(self.snp.bottom).offset(-10 + 37)
            $0.left.equalTo(self.snp.centerX).offset(10)
            $0.width.equalTo(108)
        }
        
        snp.remakeConstraints { make in
            make.width.height.equalTo(textLabel)
            make.bottom.equalTo(superview!.snp.top).offset(-3)
            make.centerX.equalToSuperview()
        }
        
        changePointImageLevel()
    }

    func setupUI() {

        self.insertSubview(self.waterView, at: 0)
        
        self.addSubview(self.imageView)
        self.addSubview(self.backView)
        self.addSubview(self.placeLabel)
        self.addSubview(self.walkNavView)
        self.backView.addSubview(self.limitParkingIcon)
        self.backView.addSubview(self.textLabel)
        self.backView.addSubview(self.limitParkingTip)
        
        textLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)

        self.backView.snp.makeConstraints {
            $0.bottom.equalToSuperview().offset(-6)
            $0.centerX.equalToSuperview()
        }

        self.limitParkingIcon.snp.makeConstraints {
            $0.trailing.equalTo(self.textLabel.snp.leading).offset(-4)
            $0.centerY.equalToSuperview()
            $0.size.equalTo(CGSize(width: 34, height: 34))
        }

        self.textLabel.snp.makeConstraints {
            $0.leading.equalToSuperview().offset(24)
            $0.trailing.equalToSuperview().offset(-24)
            $0.top.equalToSuperview().offset(8)
            $0.bottom.equalToSuperview().offset(-22)
            $0.height.equalTo(20)
        }

        self.limitParkingTip.snp.makeConstraints {
            $0.leading.equalTo(self.textLabel)
            $0.top.equalTo(self.textLabel.snp.bottom)
            $0.trailing.lessThanOrEqualToSuperview().offset(-12)
            $0.height.equalTo(14)
        }
        
        self.walkNavView.snp.makeConstraints {
            $0.bottom.equalToSuperview().offset(-6)
            $0.centerX.equalToSuperview()
            $0.size.equalTo(CGSize(width: 36, height: 30))
        }
    }
    
    func update(argvs: [String: Any]?) {
        if let jsonArgvs = argvs, let newOptions = Options(json: jsonArgvs){
            
            if newOptions.imageName != options.imageName {
//                updateAnnotationImage(with: newOptions.imageName)
                imageView.image = newOptions.image
                self.options.imageName = newOptions.imageName
            }

            if let annonationText = newOptions.text, annonationText != options.text {
               updateText(with: annonationText)
                self.options.text = annonationText
            }

            if newOptions.waterAnimationMultiple != options.waterAnimationMultiple {
                updateWaterAnimation(with: newOptions.realAnimationMultiple)
                self.options.waterAnimationMultiple = newOptions.waterAnimationMultiple
            }
        }
    }
    
    /// 更新气泡文案
    /// - Parameter text: 文案
    func updateText(with text: String?) {
        guard let tempText = text else {
            backView.isHidden = true
            return
        }

        textLabel.text = tempText
        backView.isHidden = false
    }

    /// 更新地址
    /// - Parameter text: 地址
    func updateAddress(with text: String?) {
        guard let address = text else {
            placeLabel.isHidden = true
            return
        }
        placeLabel.isHidden = false
        placeLabel.text = address
    }

    /// 更新大头针图片
    /// - Parameter imageName: 图片资源名
    func updateAnnotationImage(with imageName: String? = "location") {
        guard let tempImageName = imageName,
              let tempImage = UIImage(named: tempImageName),
              let originView = self.superview else {
            return
        }

//        imageView.image = tempImage
//        options.imageSize = tempImage.size
//        originView.set(centerOffset: CGPoint(x: 0, y: -tempImage.size.height / 2))
        self.waterView.snp.remakeConstraints { make in
            make.centerX.equalTo(originView)
            make.centerY.equalTo(originView).offset(tempImage.size.height / 2)
            make.width.height.equalTo(362)
        }
    }
    
    func updateWalkNav() {
        guard let url = options.guideMainUrl else {
            self.walkNavView.isHidden = true
            self.walkNavView.removeFromSuperview()
            return
        }
        self.walkNavView.isHidden = false
        walkNavView.updateImage(url: url, jumpUrl: options.guidePageUrl)
        self.walkNavView.removeFromSuperview()
        if self.backView.isHidden == true {
            //添加到self上
            self.addSubview(self.walkNavView)
            self.walkNavView.snp.remakeConstraints {
                $0.bottom.equalToSuperview()
                $0.centerX.equalToSuperview()
                $0.size.equalTo(CGSize(width: 36, height: 30))
            }
        }else {
            //添加气泡上
            self.backView.addSubview(self.walkNavView)
            if self.limitParkingIcon.isHidden == true && self.limitParkingTip.isHidden == true{
                ///没有限停
                self.backView.layer.cornerRadius = 23
                self.walkNavView.snp.remakeConstraints {
                    $0.trailing.equalTo(self.textLabel.snp.leading).offset(-4)
                    $0.centerY.equalToSuperview()
                    $0.size.equalTo(CGSize(width: 36, height: 30))
                }
                
                self.textLabel.snp.updateConstraints {
                    $0.top.equalToSuperview().offset(13)
                    $0.leading.equalToSuperview().offset(52)
                    $0.bottom.equalToSuperview().offset(-13)
                }
            }else {
                self.limitParkingIcon.isHidden = true
                self.backView.layer.cornerRadius = 53/2
                self.walkNavView.snp.remakeConstraints {
                    $0.trailing.equalTo(self.textLabel.snp.leading).offset(-4)
                    $0.centerY.equalToSuperview()
                    $0.size.equalTo(CGSize(width: 36, height: 30))
                }
                
                self.textLabel.snp.updateConstraints {
                    $0.top.equalToSuperview().offset(8)
                    $0.leading.equalToSuperview().offset(52)
                    $0.bottom.equalToSuperview().offset(-25)
                }
            }
        }
    }
    
    func changePointImageLevel() {
        if let sView = superview {
            sView.sendSubviewToBack(self)
        }
    }

    /// 更新水波纹动画倍数
    /// - Parameter multiple: 倍数
    func updateWaterAnimation(with multiple: CGFloat? = 1.0, pulesColor: UIColor? = nil) {
        waterView.beginRipples(with: multiple, pulseColor: pulesColor)
    }

    /// 更新限停信息
    /// - Parameter tips: 限停信息
    func updateLimitStopTips(tips: String?) {
        if let tempTips = tips, tempTips.count > 0 {
            self.backView.layer.cornerRadius = 26
            self.limitParkingIcon.isHidden = false
            self.limitParkingTip.isHidden = false
            self.limitParkingTip.text = tempTips
            self.textLabel.textAlignment = .left
            self.textLabel.snp.updateConstraints {
                $0.leading.equalToSuperview().offset(48)
                $0.bottom.equalToSuperview().offset(-22)
            }
        } else {
            self.backView.layer.cornerRadius = 18
            self.limitParkingIcon.isHidden = true
            self.limitParkingTip.isHidden = true
            self.textLabel.textAlignment = .center
            self.textLabel.snp.updateConstraints {
                $0.leading.equalToSuperview().offset(24)
                $0.bottom.equalToSuperview().offset(-8)
            }
        }
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var limitParkingIcon: UIImageView = {
        let imageView = UIImageView(image: UIImage.t3_assetsWith(T3AssetsName.icon_parking))
        return imageView
    }()

    lazy var textLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.textColor = UIColor(inner: "02102A")
        label.textAlignment = NSTextAlignment.center
        label.font = UIFont(name: "PingFangSC-Medium", size: 14)
        return label
    }()

    lazy var limitParkingTip: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(inner: "FA6400")
        label.font = UIFont(name: "PingFangSC-Medium", size: 10)
        return label
    }()

    lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.addT3Shadow()
        view.layer.cornerRadius = 18
        view.isHidden = true
        return view
    }()
    
    lazy var imageView = UIImageView()

    lazy var waterView: WaterAnnmtionView = {
        let width: CGFloat = 362.0
        let waterRect = CGRect(x: 0, y: 0, width: width, height: width)
        let waterView = WaterAnnmtionView(frame: waterRect)
        return waterView
    }()
    
    lazy var placeLabel: OutlineLable = {
        var label = OutlineLable()
        label.font = UIFont(name: "PingFangSC-Medium", size: 12)
        label.textColor = UIColor(inner: "1d2232")
        label.numberOfLines = 2
        return label
    }()
    
    private lazy var walkNavView: T3WalkNavPinView = {
        let view = T3WalkNavPinView(pageSource: .orderWaiting)
        view.isHidden = true
        view.clickWalkNav = { [weak self] in
            self?.annotationDidClicked?(nil)
        }
        return view
    }()
    
//    lazy var imageView: UIImageView = UIImageView()
    
    // 点击区域超出父视图范围, 需做转移坐标处理
    public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if walkNavView.isHidden == false {
            let subViewPoint = walkNavView.convert(point, from: self)
            if walkNavView.bounds.contains(subViewPoint) {
                return true
            }
        }
        return super.point(inside: point, with: event)
    }
}

class WaterAnnmtionView: UIView {

    private lazy var replicatorLayer = CAReplicatorLayer()
    public private(set) var isAnimationing: Bool = false

    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(radarImageView)
        radarImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: 215, height: 215))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func beginRipples(with multiple: CGFloat? = 1.0, pulseColor: UIColor? = nil) {
        if let pulseColor {
            // 自动驾驶
            radarImageView.isHidden = true
            autoPolitAnimation(with: multiple, pulseColor: pulseColor)
        } else {
            // 普通订单
            radarImageView.isHidden = false
            regularAnimation(with: multiple)
        }
    }
    
    func autoPolitAnimation(with multiple: CGFloat? = 1.0, pulseColor: UIColor) {
        self.endRipples()
        
        self.layer.backgroundColor = UIColor.clear.cgColor
        let pulseLayer = CAShapeLayer()
        pulseLayer.frame = self.layer.bounds
        pulseLayer.path = UIBezierPath(ovalIn: pulseLayer.bounds).cgPath
        pulseLayer.fillColor = (pulseColor).cgColor
        pulseLayer.opacity = 0.0
        
        self.replicatorLayer.frame = self.bounds
        self.replicatorLayer.instanceCount = 2
        self.replicatorLayer.instanceDelay = 5.4 / (multiple ?? 1.0)
        self.replicatorLayer.addSublayer(pulseLayer)
        layer.addSublayer(self.replicatorLayer)
        
        let opacityAnima = CABasicAnimation(keyPath: "opacity")
        opacityAnima.fromValue = 0.3
        opacityAnima.toValue = 0.0
        
        let scaleAnima = CABasicAnimation(keyPath: "transform")
        scaleAnima.fromValue = NSValue(caTransform3D: CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0))
        scaleAnima.toValue = NSValue(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.0, 1.0, 0.0))
        
        let groupAnima = CAAnimationGroup()
        groupAnima.animations = [opacityAnima, scaleAnima]
        groupAnima.duration = 6.2 / (multiple ?? 1.0)
        groupAnima.speed = 1.8
        groupAnima.beginTime = 1.0
        groupAnima.repeatCount = HUGE
        pulseLayer.add(groupAnima, forKey: "groupAnimation")
        self.isAnimationing = true
    }
    
    func regularAnimation(with multiple: CGFloat? = 1.0) {
        self.endRipples()

        self.layer.backgroundColor = UIColor.clear.cgColor
        let pulseLayer = CAShapeLayer()
        pulseLayer.frame = self.layer.bounds
        pulseLayer.path = UIBezierPath(ovalIn: pulseLayer.bounds).cgPath
        pulseLayer.fillColor = UIColor(inner: "2DE0A2", alpha: 0.15).cgColor
        
        pulseLayer.opacity = 0.0

        self.replicatorLayer.frame = self.bounds
        self.replicatorLayer.instanceCount = 2
        self.replicatorLayer.instanceDelay = 5.4 / (multiple ?? 1.0)
        self.replicatorLayer.addSublayer(pulseLayer)
        layer.addSublayer(self.replicatorLayer)

        let groupAnimationDuration = 1.5 / (multiple ?? 1.0)
        
        let opacityAnimaBegin = 0.7 * groupAnimationDuration
        let opacityAnimaDuration = groupAnimationDuration - opacityAnimaBegin
        let opacityAnima = CABasicAnimation(keyPath: "opacity")
        opacityAnima.fromValue = 0.7
        opacityAnima.toValue = 0.0
        opacityAnima.beginTime = opacityAnimaBegin
        opacityAnima.duration = opacityAnimaDuration
        opacityAnima.fillMode = .backwards
        
        let scaleAnima = CABasicAnimation(keyPath: "transform")
        scaleAnima.fromValue = NSValue(caTransform3D: CATransform3DScale(CATransform3DIdentity, 0.0, 0.0, 0.0))
        scaleAnima.toValue = NSValue(caTransform3D: CATransform3DScale(CATransform3DIdentity, 1.5, 1.5, 0.0))

        let groupAnima = CAAnimationGroup()
        groupAnima.animations = [opacityAnima, scaleAnima]
        groupAnima.duration = groupAnimationDuration
        groupAnima.speed = 1
        groupAnima.beginTime = 0.3
        groupAnima.duration = groupAnimationDuration
        groupAnima.repeatCount = HUGE
        pulseLayer.add(groupAnima, forKey: "groupAnimation")
        
        setupRadarAnimation(duration: groupAnimationDuration)
        
        self.isAnimationing = true
    }
    
    func setupRadarAnimation(duration: Double) {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotateAnimation.toValue = NSNumber(value: Double.pi * 2)
        rotateAnimation.duration = duration
        rotateAnimation.beginTime = 0
        rotateAnimation.timingFunction = CAMediaTimingFunction(name: .linear)
        rotateAnimation.repeatCount = HUGE   // 无限大重复次数
        rotateAnimation.isRemovedOnCompletion = false
        rotateAnimation.fillMode = .forwards
        radarImageView.layer.add(rotateAnimation, forKey: "rotation")
    }

    public func endRipples() {
        radarImageView.layer.removeAllAnimations()
        replicatorLayer.sublayers?.removeAll()
        replicatorLayer.removeFromSuperlayer()
        isAnimationing = false
    }
    
    var radarImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "map_circle_calling"))
        return imageView
    }()
}

public enum WalkNavPageSource {
    case unknow
    case home
    case mapAddress
    case orderWaiting
    case pickUp
}

//extension WalkNavPageSource {
//    var smpCode: String {
//        switch self {
//        case .unknow:
//            return ""
//        case .home:
//            return "0199.005.005.014"
//        case .mapAddress:
//            return "0199.007.002.004"
//        case .orderWaiting:
//            return "0199.012.001.010"
//        case .pickUp:
//            return "0199.013.001.016"
//        }
//    }
//}

public class T3WalkNavPinView: UIView {
    
    ///点击步行导览  可以用于埋点
    public var clickWalkNav: (() -> Void)?
    ///曝光
    public var exposeView: (() -> Void)?
    /// 跳转的url
    private var jumpUrl: String?
    private var source: WalkNavPageSource = .unknow
    
    private lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = abs(CGFloat(Int(4 * 100)) / 100)
        view.clipsToBounds = true
        view.isUserInteractionEnabled = true
        return view
    }()
    
    private lazy var textLabel: InsetLabel = {
        let label = InsetLabel()
        label.inset = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
        label.textAlignment = .center
        label.text = "图文步导"
        label.textColor = .white
        label.font = UIFont(name: "PingFangSC-Regular", size: 8)
        label.backgroundColor = UIColor(inner: "02102A", alpha: 0.7)
        return label
    }()
    
    private override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupUILayout()
    }
    
    public convenience init(pageSource: WalkNavPageSource) {
        self.init(frame: CGRect.zero)
        self.source = pageSource
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.layer.cornerRadius = 4
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.white.cgColor
        self.layer.masksToBounds = true
        self.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(clickView))
        self.addGestureRecognizer(tap)
        addSubview(imageView)
        addSubview(textLabel)
    }
    
    func setupUILayout() {
        
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        textLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-1)
            make.height.equalTo(13)
        }
    }

    @objc
    func clickView() {
        clickWalkNav?()
//        T3SASDK.trackClickWith(spmCode: self.source.smpCode)
//        guard let url = jumpUrl, let _ = URL(string: url) else {
//            T3Log.debug("跳转链接错误 url=\(jumpUrl)", tag: "walkNav")
//            return
//        }
//        service_business.deeplink(url, params: nil)
    }
    
    public func updateImage(url: String, jumpUrl: String?) {
        self.imageView.kf.setImage(with: URL(string: url), placeholder: UIImage.t3_assetsImage(name: T3AssetsName.icon_walk_nav_default.value))
        self.jumpUrl = jumpUrl
        //曝光
//        T3SASDK.trackExposureWith(spmCode: self.source.smpCode)
    }
}

///带边框的 label
class OutlineLable: UILabel {

    //轮廓的颜色
    public var outLineColor = UIColor.white
    //轮廓的宽度
    public var linWidth = 4

    //重写该方法，修改lable文字的描绘方式
    override public func drawText(in rect: CGRect) {
        let color = textColor
        let context = UIGraphicsGetCurrentContext()
        context?.setLineWidth(CGFloat(linWidth))
        context?.setLineJoin(.round)
        context?.setTextDrawingMode(.stroke)
        textColor = outLineColor
        //调用父类该方法画文字
        super.drawText(in: rect)
        //重新设置绘制模式为fill
        context?.setTextDrawingMode(.fill)
        //设置原本颜色
        textColor = color
        //重新画文字
        super.drawText(in: rect)
    }
}

class InsetLabel: UILabel {
    var inset: UIEdgeInsets = .zero

    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: inset))
    }

    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + inset.left + inset.right,
                      height: size.height + inset.top + inset.bottom)
    }
}

extension UIColor {
    convenience init(inner hex: String) {
        self.init(inner: hex, alpha: 1)
    }

    convenience init(inner hex: String, alpha: CGFloat = 1) {
        var red: CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue: CGFloat = 0.0
        var value = hex
        var nAlpha = alpha
        if hex.hasPrefix("#") {
            let index = hex.index(hex.startIndex, offsetBy: 1)
            value = String(hex[index...])
        }

        let scanner = Scanner(string: value)
        var hexValue: CUnsignedLongLong = 0
        if scanner.scanHexInt64(&hexValue) {
            switch value.count {
            case 3:
                red = CGFloat((hexValue & 0xF00) >> 8) / 15.0
                green = CGFloat((hexValue & 0x0F0) >> 4) / 15.0
                blue = CGFloat(hexValue & 0x00F) / 15.0
            case 4:
                red = CGFloat((hexValue & 0xF000) >> 12) / 15.0
                green = CGFloat((hexValue & 0x0F00) >> 8) / 15.0
                blue = CGFloat((hexValue & 0x00F0) >> 4) / 15.0
                nAlpha = CGFloat(hexValue & 0x000F) / 15.0
            case 6:
                red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
                green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
                blue = CGFloat(hexValue & 0x0000FF) / 255.0
            case 8:
                red = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
                green = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
                blue = CGFloat(hexValue & 0x000000FF) / 255.0
                nAlpha = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
            default:
                break
            }
        }
        self.init(red: red, green: green, blue: blue, alpha: nAlpha)
    }
}

extension UIView {
    func addCorner(conrners: UIRectCorner , radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
    
    func addT3Shadow(with color: UIColor = UIColor(inner: "313854"),
                     offsetY: CGFloat = -4,
                     cornorRadius: CGFloat = 8,
                     alpha: CGFloat = 0.08) {
        self.layer.shadowOffset = CGSize(width: 0, height: offsetY)
        self.layer.shadowOpacity = 1
        self.layer.shadowColor = color.cgColor.copy(alpha: alpha)
        self.layer.shadowRadius = cornorRadius
    }

    func removeShadow() {
        layer.shadowOpacity = 0
    }
}

